package com.example.kabaouser.controller;

import com.example.kabaouser.Utils.QiniuUtil;
import com.example.kabaouser.config.QiniuConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/pic")
public class PicController {

    @Autowired
    private QiniuConfig qiniuConfig;

    @PostMapping(value = "/up",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity updatePhoto(@RequestParam("file") MultipartFile file) throws IOException{
        QiniuUtil qiniu = new QiniuUtil();
        FileInputStream inputStream = (FileInputStream) file.getInputStream();
        String path = qiniu.uploadQNImg(inputStream,"test_" + UUID.randomUUID(),qiniuConfig);
        return new ResponseEntity(path, HttpStatus.OK);
    }
}
